package controller

import (
	"context"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"sgo/app/client/model"
	"sgo/common/response"
	"sgo/scontext"
)

// 测试redis客户端连接

type RedisController struct {
}

func (RedisController) StringSetAndGet(ctx *gin.Context, sc *scontext.SgoContext) response.Response {

	background := context.Background()
	s := new(model.StringSet)

	_ = ctx.ShouldBindJSON(s)

	err := sc.Validator.Validate.Struct(s)
	if err != nil {
		return response.Error(err)
	}

	_, err = sc.RedisClient.Set(background, s.Key, s.Val, 0).Result()
	if err != nil {
		sc.Logger.Error("添加失败", zap.Error(err))
		return response.Error(err)
	}

	result, err := sc.RedisClient.Get(background, s.Key).Result()
	if err != nil {
		return response.Error(err)
	}

	return response.OkWithData(result)
}
